$(document).ready(function(){
	function init(){
		$('#loginBtn').on('click', function() {
			login();
		});
	}

	function login(){
		var data = formData();
		var url = "/api/system/login";
		clearMsg();
		//表单数据提交到/api/system/login
		$.ajax({
				type : 'POST',
				url : url,
				dataType : 'json',
				contentType : 'application/json',
				data : JSON.stringify(data),
				success : function(responseData) {
					//console.log(responseData);
					if (responseData.success === true) {
						alert("登录成功.");
						window.location.href = "/home";
					} else {
						//服务端有效性检查后在ui的呈现
						var msgs = responseData.data;
						for(var i = 0;i<msgs.length;i++){
							$('#'+msgs[i].id).html(msgs[i].msg);
						}
					}
				}
			});
	}
	//收集表单数据并返回对象
	function formData(){
		return {
			name:$('#name').val(),
			pwd:$('#pwd').val()
		}
	}
	function clearMsg(){
		$('.msg').html('');
	}

	init();
});